<?php
	class Front_Page extends MY_Controller {
		function Front_Page() {
			parent::__construct();
			$this->load->library('session');
			@session_start();
		}	
		
		function index () {
			$data['title'] = "Trang chủ";
			$data['content'] = 'front_page/content/home';
			
			//lay ra link lien ket HBA
			$this->db->where('order < 6');
			$this->db->where('status = 1');
			$this->db->order_by('order','ASC');
			$data['record_5'] = $this->db->get('link_hba')->result_array();
			
			$this->db->where('order > 5');
			$this->db->where('status = 1');
			$this->db->order_by('order','ASC');
			$data['record_more'] = $this->db->get('link_hba')->result_array();
	
			//lay ra 11 danh muc san pham cap 1 dau tien
			$this->db->where('order < 12');
			$this->db->where('level = 1');
			$this->db->where('status = 1');
			$this->db->order_by('order','ASC');
			$data['record_ctg_1'] = $this->db->get('category_product')->result_array();
		
			//lay ra danh muc san pham cap 1 tiep theo
			$this->db->where('order > 11');
			$this->db->where('level = 1');
			$this->db->where('status = 1');
			$this->db->order_by('order','ASC');
			$data['record_ctg_1_more'] = $this->db->get('category_product')->result_array();
			
			//lay ra danh muc san pham cap 2
			$this->db->where('level = 2');
			$this->db->where('status = 1');
			$this->db->order_by('order','ASC');
			$data['record_ctg_2'] = $this->db->get('category_product')->result_array();
			
			//lay ra cac san pham hot
			$this->db->select('product.name, product.image, product_hot.id');
			$this->db->from('product_hot');
			$this->db->join('product', 'product.id = product_hot.product_id');		
			$data['prd_hot'] = $this->db->get()->result_array();
			
			//lay ra cac san pham quang cao ADS
			$this->db->select('product.name, product.image, product_ads.id');
			$this->db->from('product_ads');
			$this->db->join('product', 'product.id = product_ads.product_id');		
			$data['prd_ads'] = $this->db->get()->result_array();
			
			//lay ra cac danh muc san pham hien thi tren trang chu
			$this->db->order_by('order','ASC');
			$data['prd_cate'] = $this->db->get_where('category_product',array('active' => 1))->result_array();
			
			//lay ra so san pham di kem danh muc
			//$this->db->order_by('order','ASC');
			//$data['prd_cate'] = $this->db->get_where('category_product',array('active' => 1))->result_array();
			
			//lay ra nhom nguoi dung
			$data['group_user'] = $this->db->get('group_user')->result_array();
			
			$this->load->view('front_page/template_home',$data);
		}
		
		function search () {
			$data['title'] = "Search";
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay ra danh muc san pham 
			$data['category_product_search'] = $this->db->get('category_product')->result_array();
			
			// gia tri lay tu form
			$check_search = $this->input->POST('key_search');
			
			// lay ra cac san pham tim kiem
			if ($this->input->POST('cate_search')) {
				$where = array(
					'category_product_id' => $this->input->POST('cate_search')
				);	
				$data['cate_search'] = $this->input->POST('cate_search');
			} else {
				$where = array();
				$data['cate_search'] = ' ';
			}
			
			if(!empty($check_search)){
				$like = array('name' =>$this->input->POST('key_search'));
				$data['name'] = $this->input->POST('key_search'); 																												
			} else {
				$like = array();
				$data['name'] = ' ';
			}
			
			if ($this->input->POST('cost_search')) {
				$sort = array(
					'cost' => $this->input->POST('cost_search')
				);	
				$data['cost'] = $this->input->POST('cost_search'); 
			} else {
				$sort = array("id"=>"desc");
				$data['cost'] = ' ';
			}
			$data['data_search'] = $this->mod_lib->search('product','',$like ,$where,$sort);
			
			//phan trang 
			$per_page = 15;
			$data_search = $this->mod_lib->search('product','',$like ,$where,$sort);
			$data['src_product'] = $this->mod_lib->load_search('product','',$like,$where,$per_page,$this->uri->segment(4),$sort);		
			$config['base_url']     =  site_url('front_page/front_page/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			
			// hien thi noi dung
			$data['content'] = 'front_page/search_product';
			$this	->load->view('front_page/template_frontpage_3',$data);
		} 
		
		function prd_hot(){
			$data['title'] = "Sản phẩm nổi bật";

			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// san pham lien quan (san pham cung danh muc) 
			$this->db->where('category_product_id', $data['product_detail']['category_product_id']);
			$data['product_lq']   = $this->db->get('product')->result_array();
			
			$data['content'] = 'front_page/prd_hot';
			$this->load->view('front_page/template_frontpage_2',$data);
		}
		
		function cart($id) {
			
			 if(isset($id) AND $id!=''){	 	
			 	if(isset($_SESSION['cart_prd'])) {
			 		$prd_all = $_SESSION['cart_prd'];
			 		foreach ($prd_all as $key_prd => $value_prd) {
			 			if($key_prd == $id) {
			 				 $_SESSION['cart_prd'][$key_prd]['quantity'] = $value_prd['quantity'] + 1;
			 				 redirect('front_page/front_page/cart_detail'); 
			 			}
			 		}		
			 	}
			 	
			 	// lay ra san pham 
			 	$this->db->where('id', $id);
				$data['product_detail']   = $this->db->get('product')->row_array();
				
				$_SESSION['cart_prd'][$data['product_detail']['id']] = array(
											'id'					=>$data['product_detail']['id'],
	                                        'category_product_id'	=>$data['product_detail']['category_product_id'],
	                                        'name'					=>$data['product_detail']['name'],
											'cost'					=>$data['product_detail']['cost'],
											'cost_new'				=>$data['product_detail']['cost_new'],
	                                        'product_code'		    =>$data['product_detail']['product_code'],
											'image'	                =>$data['product_detail']['image'],
											'quantity'              =>1 
                                        );
                                       
                redirect('front_page/front_page/cart_detail');                        
			 }
		} 
		
		function cart_detail () {
			$data['title'] = "Giỏ hàng";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();

			if (isset($_SESSION['cart_prd'])){
				$data['cart_prd_detail'] = $_SESSION['cart_prd'];
			} else {
				$data['cart_prd_detail'] = array();
			}
				
			$data['content']  = 'front_page/cart_detail';
			$this->load->view('front_page/template_frontpage',$data);
		}
		
		function update_cart() {
			$cart = $this->input->post("cart_update");
			$up_cart = explode("-", $cart);
			$prd_all = $_SESSION['cart_prd'];		
			$_SESSION['cart_prd'][$up_cart[1]]['quantity'] = $up_cart[2];
	
			redirect('front_page/front_page/cart_detail'); 
		}
		
		function delete_cart() {
			$cart_delete = $this->input->post("cart_delete");
			unset($_SESSION['cart_prd'][$cart_delete]);

			redirect('front_page/front_page/cart_detail');
		}
		
		function send_order () {
			$data['title'] = "Gửi đơn hàng";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			
			if (isset($_SESSION['user_name'])) {
				$data['content']  = 'front_page/send_order';
				
				if (isset($_SESSION['cart_prd'])){
					$data['cart_prd_detail'] = $_SESSION['cart_prd'];
				} else {
					$data['cart_prd_detail'] = array();
				}
			} else {
				$data['content']  = 'front_page/login_order';
			}
			
			$this->load->view('front_page/template_frontpage',$data);
		}
		
		function check_form_older() {
			$sumit_login = $this->input->post("submit");
			$username = $this->input->post("username");
			$password = md5($this->input->post("password"));
  			
			if(!empty($sumit_login)) {
				$value_user = $this->db->get_where('user',array('user_name'=> $username, 'password'=>$password));
				
				if ($value_user->num_rows() > 0) {
					$row = $value_user->row_array();
					$_SESSION['user_name']			= $row['user_name'];
					$_SESSION['error_loginadmin']	= '';
					
					$_SESSION['user_info'] = $row;
				} else {
					$_SESSION['error_loginadmin']	= '(Bạn đăng nhập sai tài khoản !)';
					
				}	
			}	
			
			redirect('front_page/front_page/send_order');	
		}
		
		function active_order () {
			$data['title'] = "Đăng ký";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			$check = $this->input->post('submit');
			
			if($check){
				$data_value_order = array (
							'id_user'                    =>$_SESSION['user_info']['id'],
							'created_date'               =>date ("Y-m-d H:m:s"),
							'name_user'                  =>$_SESSION['user_info']['full_name'],
							'requirements'               =>$this->input->post("requirements")
						);
					
				$this->mod_lib->insert('order',$data_value_order);
				
				$id_order = $this->db->insert_id();
				$where = array(
			  			'id' => $id_order  
		  		);
			  		
			  	$order = array(
			  			'id' => 'DESC' 
		  		);
		  		
		  		$data_value_order_last = array (
						'order_code'                    => $this->db->insert_id().'HD'.$_SESSION['user_info']['id']
				);
				
				$this->mod_lib->update('order',$where,$order,$data_value_order_last);
				
				foreach ($_SESSION['cart_prd'] as $cart_prdall) {
					
					$data_value_order_detail = array (
							'product_id'             =>$cart_prdall['id'],
							'order_id'               =>$id_order,
							'quantity'				 =>$_SESSION['cart_prd'][$cart_prdall['id']]['quantity']	
						);
						
					$this->mod_lib->insert('order_detail',$data_value_order_detail);
				}
				
				$data['content'] = 'front_page/active_order_successful';	
			} else {
				$data['content'] = 'front_page/active_order_error';	
			}	
			
			$this->load->view('front_page/template_frontpage',$data);		
		}
		
		function detail ($id) {
			$data['title'] = "Hamo";
			//cap cha
			$data['captcha'] = $this->get_random_color();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay san pham tuong ung
			// thong tin chung
			$this->db->where('id', $id);
			$data['product_detail']   = $this->db->get('product')->row_array();
		
			// cac anh san pham
			$this->db->where('product_id', $id);
			$data['product_img']   = $this->db->get('product_images')->result_array();

			//lay ra cac nhan vien ho tro khach hang
			$this->db->where('position', 'kd');
			$data['product_kd']   = $this->db->get('yahoo')->result_array();
			$this->db->where('position', 'kt');
			$data['product_kt']   = $this->db->get('yahoo')->result_array();
			
			//lay ra cay danh muc root_path
			$this->db->select('root_path');
			$this->db->where('id',$data['product_detail']['category_product_id']);
			$data['category_product_row'] = $this->db->get('category_product')->row_array();
			
			// cac thuoc tinh san pham
			$sql =  "SELECT category_product_pp.properties_id, category_product_pp.product_id, properties.name, category_product_pp.value 
					 FROM   category_product_pp,properties
				 	 WHERE  category_product_pp.product_id = $id
				 	 AND    category_product_pp.properties_id = properties.id
				 	";
			$data['product_pp']  = $this->db->query($sql)->result_array();

			// lay ra cac danh gia san pham 
			$this->db->where('product_id',$id);
			$data['cm'] = $this->db->get('cm')->result_array();
			$sql_cm = "SELECT avg(star) AS star_tb FROM cm WHERE product_id = $id";
			$data['cm_tb'] = $this->db->query($sql_cm)->row_array();
			
			// san pham lien quan (san pham cung danh muc) 
			$this->db->where('category_product_id', $data['product_detail']['category_product_id']);
			$data['product_lq']   = $this->db->get('product')->result_array();
		
			$data['content'] = 'front_page/detail';
			$this->load->view('front_page/template_frontpage_2',$data);
		}
		
		function detail_nb($id_category) {
			$data['title'] = "Trang chủ Hamo";
			//hien thi
			$data['content'] = 'front_page/detail_nb';
			
			//lay ra cay danh muc root_path
			$this->db->select('root_path');
			$this->db->where('id', $id_category);
			$data['category_product_row'] = $this->db->get('category_product')->row_array();
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay ra so san pham noi bat tuong ung voi danh muc
			$this->db->where('category_product_id', $id_category);
			$data['prd_category']   = $this->db->get('product',3,0)->result_array();
			
			// phan trang san pham - lay tat ca san 
			$per_page = 6;
			$uri = 4;
			
			$data['product_dtnb']   =  $this->mod_lib->load_all('product','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url("front_page/front_page/detail_nb/$id_category");
			$config['total_rows']   =  count($data['product_dtnb']);   
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);
			
			
			$this->load->view('front_page/template_frontpage_3',$data);
		}
		
		function login () {
			$sumit_login = $this->input->post("submit");
			$username = $this->input->post("username");
			$password = md5($this->input->post("password"));
  			
			if(!empty($sumit_login)) {
				$value_user = $this->db->get_where('user',array('user_name'=> $username, 'password'=>$password));
				
				if ($value_user->num_rows() > 0) {
					$row = $value_user->row_array();
					$_SESSION['user_name']			= $row['user_name'];
					$_SESSION['error_loginadmin']	= '';
					
					$_SESSION['user_info'] = $row;
				} else {
					$_SESSION['error_loginadmin']	= '(Bạn đăng nhập sai tài khoản !)';	
				}	
			}	
			
			redirect('front_page/front_page');
		}
		
		function login_ed () {
			$sumit_login = $this->input->post("submit");
			$username = $this->input->post("username");
			$password = md5($this->input->post("password"));
  			
			if(!empty($sumit_login)) {
				$value_user = $this->db->get_where('user',array('user_name'=> $username, 'password'=>$password));
				
				if ($value_user->num_rows() > 0) {
					$row = $value_user->row_array();
					$_SESSION['user_name']			= $row['user_name'];
					$_SESSION['error_loginadmin_ed']	= '';
					
					$_SESSION['user_info'] = $row;
				} else {
					$_SESSION['error_loginadmin_ed']	= '(Bạn đăng nhập sai tài khoản !)';	
				}	
			}	
			
			redirect('front_page/front_page');
		}
		
		function logout () {
			session_destroy();
			redirect('front_page/front_page');
		}
		
		function edit_user($id) {
			$data['title'] = "Thay đổi thông tin tài khoản";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			
   			// lay san pham noi bat
   			$this->db->where('hot', '1');
			$data['product_hot']   = $this->db->get('product')->result_array();
			
			// lay san pham khuyen mai
			$this->db->where('promotion', '1');
			$data['product_promotion']   = $this->db->get('product')->result_array();
			
			// hien thi noi dung
			$data['content'] = 'front_page/edit_user';
			
			// captcha
			$data['captcha'] = $this->get_random_color();

			// thong tin user				
			$data['value_user'] = $this->db->get_where('user',array('id'=> $id))->row_array();
			
			$sub_register = $this->input->post('sub_register');
			
			if (!isset($_SESSION['user_info'])){
				redirect('front_page/front_page/register');	
			}
			
			if(!empty($sub_register )){		
				$where = array(
			  			'id' => $id 
		  		);
			  		
			  	$order = array(
			  			'id' => 'DESC' 
		  		);
		  		
		  		// lay ra cac user
				$user_arr   = $this->db->get('user')->result_array();
				
				$check = 0;
				
				foreach ($user_arr as $user) {
					if ($user['user_name'] == trim($this->input->post('user_name')) && $user['user_name'] !== $data['value_user']['user_name']) {
						$check = 1;
					} 
				}
				
				if($this->input->post('password')) {
					$data['eror_user'] = 'Bạn nhập sai mật khẩu mới hoặc thông tin bạn nhập vào chưa đúng !';
				}
				
				if ($this->input->post('password') == $this->input->post('cf_password')) {
					if ($check == 0) {
						$data_value = array (
							'user_name'                  =>trim($this->input->post('user_name')),
							'email' 					 =>trim($this->input->post('email')),
							'password'                   =>md5(trim($this->input->post('password'))),
							'full_name' 			     =>trim($this->input->post('full_name')),
							'phone'						 =>trim($this->input->post('phone')),
							'address'				     =>trim($this->input->post('address')),
							'date'				         =>date ("Y-m-d H:m:s"),
						); 
						
						$this->mod_lib->update('user',$where,$order,$data_value);
		
						$row = $this->db->get_where('user',array('id'=> $id))->row_array();
						$_SESSION['user_name']			= $row['user_name'];
						$_SESSION['error_loginadmin']	= '';
						$_SESSION['user_info'] = $row;
						
						$data['content'] = 'front_page/edit_user_successful';
						$this->load->view('front_page/template_frontpage',$data);
					} else {
						$data['eror_user'] = 'Tên đăng nhập đã tồn tại, bạn vui lòng chọn tên đăng nhập khác !';
					}
				} else {
					if ($check == 0) {
						$data_value = array (
							'user_name'                  =>trim($this->input->post('user_name')),
							'email' 					 =>trim($this->input->post('email')),
							'full_name' 			     =>trim($this->input->post('full_name')),
							'phone'						 =>trim($this->input->post('phone')),
							'address'				     =>trim($this->input->post('address')),
							'date'				         =>date ("Y-m-d H:m:s"),
						);
						
						$row = $this->db->get_where('user',array('id'=> $id))->row_array();
						$_SESSION['user_name']			= $row['user_name'];
						$_SESSION['error_loginadmin']	= '';
						$_SESSION['user_info'] = $row;
						
						$this->mod_lib->update('user',$where,$order,$data_value);
						$data['content'] = 'front_page/edit_user_successful';
						$this->load->view('front_page/template_frontpage',$data);
					} else {
						$data['eror_user'] = 'Tên đăng nhập đã tồn tại, bạn vui lòng chọn tên đăng nhập khác !';
					}		
				}
			}
			 
			$this->load->view('front_page/template_frontpage_3',$data);	
		} 
		
		function register () {
			$data['title'] = "Đăng ký";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			
			// hien thi noi dung
			$data['content'] = 'front_page/register';
			
			// check form 
			$sub_register = $this->input->post('sub_register');
			
			//cap cha
			$data['captcha'] = $this->get_random_color();
			
			if(!empty($sub_register )){	
				// lay ra cac user
				$user_arr   = $this->db->get('user')->result_array();
				
				$check = 0;
				
				foreach ($user_arr as $user) {
					if ($user['user_name'] == trim($this->input->post('user_name'))) {
						$check = 1;
					} 
				}
				
				if ($check == 0) {
					$data_value = array (
						'user_name'                  =>trim($this->input->post('user_name')),
						'password'                   =>md5(trim($this->input->post('password'))),
						'email' 					 =>trim($this->input->post('email')),
						'full_name' 			     =>trim($this->input->post('full_name')),
						'phone'						 =>trim($this->input->post('phone')),
						'address'				     =>trim($this->input->post('address')),
						'date'				         =>date ("Y-m-d H:m:s"),
					);

					$this->mod_lib->insert('user',$data_value );
					
					$data['content'] = 'front_page/register_successful';	
					$this->load->view('front_page/template_frontpage',$data);		
				} else {
					$data['error'] = "Tên đăng nhập đã tồn tại, bạn vui lòng chọn tên khác !";
				}	
			}
			
			$this->load->view('front_page/template_frontpage_3',$data);
		}
		
		function news($id) {	
			$data['title'] = "Trang tin tức";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			
			// phan trang tin tuc - lay tat ca cac tin 
			$per_page = 9;
			$uri = 5;
			$data['news_all']   = $this->mod_lib->load_all('news','',array('category_news_id' => $id),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url("front_page/front_page/news/$id");
			$config['total_rows']   =  count($this->db->get_where('news',array('category_news_id' => $id))->result_array());
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);

			$data['content'] = 'front_page/news';
			$this->load->view('front_page/template_frontpage',$data);		
		}
		
		function news_detail($id) {	
			$data['title'] = "Trang tin tức";
			
			// lay ra danh muc san pham 
			$data['category_product'] = $this->db->get('category_product')->result_array();
			
			// lay ra danh muc tin tuc
			$data['category_news'] = $this->db->get('category_news')->result_array();
			
			// lay san pham moi 
			$this->db->where('new', '1');
			$data['product_new']   = $this->db->get('product')->result_array();
			
			// lay ra bai viet 
			$data['news_dt'] = $this->db->get_where('news',array('id' => $id))->row_array();
			
			// phan trang tin tuc - lay tat ca cac tin 
			$per_page = 9;
			$uri = 5;
			$data['news_all']   = $this->mod_lib->load_all('news','',array('category_news_id' => $id),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url("front_page/front_page/news/$id");
			$config['total_rows']   =  count($this->db->get_where('news',array('category_news_id' => $id))->result_array());
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);

			$data['content'] = 'front_page/news_detail';
			$this->load->view('front_page/template_frontpage',$data);		
		}
		
		function comment(){
			//cap cha
			$data['captcha'] = $this->get_random_color();
			
			$star_comment = $this->input->post('star_comment');
			$name_comment = trim($this->input->post('name_comment'));
			$email_comment = trim($this->input->post('email_comment'));
			$content_comment = trim($this->input->post('content_comment'));
			$text_comment = trim($this->input->post('text_comment'));
			$id_product   = $this->input->post('id_product');
			$hidden_comment = $this->input->post('hidden_comment');
			$ip =  $_SERVER['REMOTE_ADDR'];			

			//lay cac record comment 
			$record_comment = $this->db->get('cm')->result_array();
			$check_cm = 0;
			
			if (count($record_comment) > 0) {
				foreach ($record_comment as $cm) {
					if($ip == $cm['ip'] && $id_product == $cm['product_id']) {
						$check_cm = 1;
					}
				}
			} else {
				$check_cm = 0;		
			}
			
			if ($check_cm == 0) {
				if (!$name_comment) {
					$data['eror_comment'] = "Bạn chưa nhập tên !<br />";
					$this->load->view('front_page/comment',$data);	
				}
				if (!$email_comment) {
					$data['eror_comment'] = "Bạn chưa nhập email !<br />";
					$this->load->view('front_page/comment',$data);	
				}
				if ($text_comment  == $hidden_comment) {
					$data_value = array (
									'name'                       =>$name_comment,	
									'product_id'                 =>$id_product,
									'email' 					 =>$email_comment,
									'content' 					 =>$content_comment,
									'star'						 =>$star_comment,		
									'ip'                         =>$ip,
									'date'				         => date ("Y-m-d H:m:s"),
								);

					$this->mod_lib->insert('cm',$data_value );
					
					$data['eror_comment'] = "Nhận xét - đánh giá của bạn đã được gửi đi !";	
				} else {
					$data['eror_comment'] = "Nhập sai mã xác nhận !";
				}
			} else {
			  $data['eror_comment'] = "Bạn đã đánh giá sản phẩm này !";
			}
			
			$this->load->view('front_page/comment',$data);	
		}
		
		// capcha
		function gen_image ($img_cm) {
			$img=ImageCreate(75,25);                       // quy dinh kich co cua anh
			$bgcolor=ImageColorAllocate($img,255,255,255); // cho mau nen là mau trang
			$red=ImageColorAllocate($img,5,5,5);         // quy dinh mau cho anh	
			ImageString($img,10,10,10, $img_cm ,$red); // add cái text lay duoc tu request vào obj image
			
			// dua ra image vua tao
			header ("Content-type: image/png");
			
			//ImageSetPixel($img,0,0,$red);   // thiet lap vi tri cua diem anh
			ImagePNG($img);                   // dua ra mot anh PNG
			ImageDestroy($img);               // xoa bo nho dem luu tru $img 
		}
		
		function get_random_color(){
			$c='';
			for ($i = 0; $i<6; $i++){
				$c .=  dechex(rand(0,15));
			}
			return "$c";
		} 
		
	}
?>